package org.example.springboot.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.example.springboot.common.Result;
import org.example.springboot.entity.SysConfig;
import org.example.springboot.service.SysConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统配置控制器
 */
@Tag(name = "系统配置接口")
@RestController
@RequestMapping("/system/config")
public class SysConfigController {

    private static final Logger LOGGER = LoggerFactory.getLogger(SysConfigController.class);
    
    @Resource
    private SysConfigService sysConfigService;
    
    /**
     * 获取所有系统配置
     * @return 配置列表
     */
    @Operation(summary = "获取所有系统配置")
    @GetMapping("/list")
    public Result<List<SysConfig>> getConfigList() {
        LOGGER.info("获取所有系统配置");
        List<SysConfig> configList = sysConfigService.getAllConfigs();
        return Result.success(configList);
    }
    
    /**
     * 根据KEY获取配置
     * @param key 配置键
     * @return 配置信息
     */
    @Operation(summary = "根据KEY获取配置")
    @GetMapping("/{key}")
    public Result<SysConfig> getConfigByKey(@PathVariable String key) {
        LOGGER.info("获取配置，KEY: {}", key);
        SysConfig config = sysConfigService.getConfigByKey(key);
        if (config == null) {
            return Result.error("配置不存在");
        }
        return Result.success(config);
    }
    
    /**
     * 更新配置
     * @param key 配置键
     * @param config 配置信息
     * @return 操作结果
     */
    @Operation(summary = "更新配置")
    @PutMapping("/{key}")
    public Result<?> updateConfig(@PathVariable String key, @RequestBody SysConfig config) {
        LOGGER.info("更新配置，KEY: {}", key);
        if (!key.equals(config.getConfigKey())) {
            return Result.error("配置键不匹配");
        }
        
        boolean success = sysConfigService.updateConfig(config);
        if (success) {
            return Result.success("更新成功");
        } else {
            return Result.error("更新失败");
        }
    }
    
    /**
     * 添加配置
     * @param config 配置信息
     * @return 操作结果
     */
    @Operation(summary = "添加配置")
    @PostMapping("/add")
    public Result<?> addConfig(@RequestBody SysConfig config) {
        LOGGER.info("添加配置，KEY: {}", config.getConfigKey());
        if (sysConfigService.getConfigByKey(config.getConfigKey()) != null) {
            return Result.error("配置键已存在");
        }
        
        boolean success = sysConfigService.addConfig(config);
        if (success) {
            return Result.success("添加成功");
        } else {
            return Result.error("添加失败");
        }
    }
    
    /**
     * 删除配置
     * @param key 配置键
     * @return 操作结果
     */
    @Operation(summary = "删除配置")
    @DeleteMapping("/{key}")
    public Result<?> deleteConfig(@PathVariable String key) {
        LOGGER.info("删除配置，KEY: {}", key);
        boolean success = sysConfigService.deleteConfig(key);
        if (success) {
            return Result.success("删除成功");
        } else {
            return Result.error("删除失败");
        }
    }
} 